package com.easy.restful.girls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.restful.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import com.easy.restful.common.core.common.pagination.Page;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.easy.restful.sys.service.ImportService;
import com.easy.restful.util.office.ExcelUtil;
import com.easy.restful.girls.model.GirlsInfo;
import com.easy.restful.girls.service.GirlsInfoService;
import com.easy.restful.girls.dao.GirlsInfoMapper;

/**
 * 测试
 *
 * @author 系统管理员
 * @date 2021-05-13
 */
@Service
public class GirlsInfoServiceImpl extends ServiceImpl<GirlsInfoMapper, GirlsInfo> implements GirlsInfoService ,ImportService {

    /**
     * 列表
     * @param object 查询条
     * @param page   分页
     * @return Page<GirlsInfo>
     */
    @Override
    public Page<GirlsInfo> select(GirlsInfo object, Page<GirlsInfo> page) {
        QueryWrapper<GirlsInfo> queryWrapper = new QueryWrapper<>();
        if(object != null){
            // 查询条件
            // NO
            if (Validator.isNotEmpty(object.getUserId())) {
                queryWrapper.like("t.user_id", object.getUserId());
            }
            // 用户名
            if (Validator.isNotEmpty(object.getUserName())) {
                queryWrapper.like("t.user_name", object.getUserName());
            }
            // 密码
            if (Validator.isNotEmpty(object.getUserPassword())) {
                queryWrapper.like("t.user_password", object.getUserPassword());
            }
        }
        page.setRecords(getBaseMapper().select(page, queryWrapper));
        return page;
    }

    /**
     * 详情
     *
     * @param id id
     * @return GirlsInfo
     */
    @Override
    public GirlsInfo input(String id) {
        ToolUtil.checkParams(id);
        return getBaseMapper().getById(id);
    }

    /**
     * 新增
     *
     * @return GirlsInfo
     */
    @Override
    public GirlsInfo add() {
        GirlsInfo object = new GirlsInfo();
        // 设置默认值
        return object;
    }

    /**
     * 删除
     *
     * @param ids 数据ids
     * @return true/false
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean remove(String ids) {
        ToolUtil.checkParams(ids);
        List<String> idList = Arrays.asList(ids.split(","));
        return removeByIds(idList);
    }

    /**
     * 保存
     *
     * @param object 表单内容
     * @return GirlsInfo
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public GirlsInfo saveData(GirlsInfo object) {
        ToolUtil.checkParams(object);
        if (Validator.isEmpty(object.getUserId())) {
            // 新增,设置默认值
        }
        return (GirlsInfo) ToolUtil.checkResult(saveOrUpdate(object), object);
    }

    /**
     * 验证数据，插入临时表后调用
     * 注: 返回false会触发异常回滚
     *
     * @param templateId 模板id
     * @param userId 用户id
     * @return true/false
     */
    @Override
    public boolean verificationData(String templateId, String userId) {
        return true;
    }

    /**
     * 导入前回调，插入正式表之前会调用此方法，建议导入正式表之前使用次方法再次验证数据，防止验证 ~ 导入之间数据发送变动
     * 注: 返回false会触发异常回滚
     *
     * @param templateId 模板id
     * @param userId 用户id
     * @return true/false
     */
    @Override
    public boolean beforeImport(String templateId, String userId) {
        return true;
    }

    /**
     * 导入后回调，插入正式表后会调用此方法
     * 注: 返回false会触发异常回滚
     *
     * @return true/false
     */
    @Override
    public boolean afterImport() {
        return true;
    }

    @Override
    public String exportData(GirlsInfo object) {
        QueryWrapper<GirlsInfo> queryWrapper = new QueryWrapper<>();
        if(object != null){
            // 查询条件
            // NO
            if (Validator.isNotEmpty(object.getUserId())) {
                queryWrapper.like("t.user_id", object.getUserId());
            }
            // 用户名
            if (Validator.isNotEmpty(object.getUserName())) {
                queryWrapper.like("t.user_name", object.getUserName());
            }
            // 密码
            if (Validator.isNotEmpty(object.getUserPassword())) {
                queryWrapper.like("t.user_password", object.getUserPassword());
            }
        }
        List<GirlsInfo> list = getBaseMapper().exportData(queryWrapper);
        return ExcelUtil.writeAndGetDownloadId("测试", "测试", list, GirlsInfo.class);
    }
}